<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Python基础知识点(爬虫之数据解析) | 小何blog (记录美好)</title><meta name="keywords" content="Python,基础知识✍️"><meta name="author" content="六根清静"><meta name="copyright" content="六根清静"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta http-equiv="Cache-Control" content="no-transform"><meta http-equiv="Cache-Control" content="no-siteapp"><meta name="description" content="Python是一门解释型，面向对象的高级编程语言。是一门开源免费的，支持交互式，可跨平台移植的脚本语言。">
<meta property="og:type" content="article">
<meta property="og:title" content="Python基础知识点(爬虫之数据解析)">
<meta property="og:url" content="https://liugenqingjing2.coding.me/2020/12/07/Python%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86%E7%82%B9-%E7%88%AC%E8%99%AB%E4%B9%8B%E6%95%B0%E6%8D%AE%E8%A7%A3%E6%9E%90/index.html">
<meta property="og:site_name" content="小何blog (记录美好)">
<meta property="og:description" content="Python是一门解释型，面向对象的高级编程语言。是一门开源免费的，支持交互式，可跨平台移植的脚本语言。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://gitee.com/he_chaoming/blog_tuchuang/raw/master/20201207/Fl4X3LG@lWEY.jpg">
<meta property="article:published_time" content="2020-12-06T16:13:18.000Z">
<meta property="article:modified_time" content="2021-01-11T03:27:51.725Z">
<meta property="article:author" content="六根清静">
<meta property="article:tag" content="Python">
<meta property="article:tag" content="基础知识✍️">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gitee.com/he_chaoming/blog_tuchuang/raw/master/20201207/Fl4X3LG@lWEY.jpg"><link rel="shortcut icon" href="https://gitee.com/he_chaoming/blog_tuchuang/raw/master/20200907/wOu*ZmuVb5A4.png"><link rel="canonical" href="https://liugenqingjing2.coding.me/2020/12/07/Python%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86%E7%82%B9-%E7%88%AC%E8%99%AB%E4%B9%8B%E6%95%B0%E6%8D%AE%E8%A7%A3%E6%9E%90/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//hm.baidu.com"/><link rel="preconnect" href="//fonts.googleapis.com" crossorigin="crossorigin"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.css"><script>var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?e829919a4d931d733ee74d5d97a51582";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
</script><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Titillium+Web&amp;display=swap"><script>var GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"search.xml","languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: {"defaultEncoding":2,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"簡"},
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: {"limitCount":20,"languages":{"author":"作者: 六根清静","link":"链接: ","source":"来源: 小何blog (记录美好)","info":"著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。"}},
  ClickShowText: undefined,
  lightbox: 'fancybox',
  Snackbar: {"chs_to_cht":"你已切换为繁体","cht_to_chs":"你已切换为简体","day_to_night":"你已切换为深色模式","night_to_day":"你已切换为浅色模式","bgLight":"#49b1f5","bgDark":"#121212","position":"bottom-left"},
  justifiedGallery: {
    js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
    css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
  },
  isPhotoFigcaption: true,
  islazyload: true,
  isanchor: true
};

var saveToLocal = {
  set: function setWithExpiry(key, value, ttl) {
    const now = new Date()
    const expiryDay = ttl * 86400000
    const item = {
      value: value,
      expiry: now.getTime() + expiryDay,
    }
    localStorage.setItem(key, JSON.stringify(item))
  },

  get: function getWithExpiry(key) {
    const itemStr = localStorage.getItem(key)

    if (!itemStr) {
      return undefined
    }
    const item = JSON.parse(itemStr)
    const now = new Date()

    if (now.getTime() > item.expiry) {
      localStorage.removeItem(key)
      return undefined
    }
    return item.value
  }
}</script><script id="config_change">var GLOBAL_CONFIG_SITE = { 
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2021-01-11 11:27:51'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(function () {  window.activateDarkMode = function () {
    document.documentElement.setAttribute('data-theme', 'dark')
    if (document.querySelector('meta[name="theme-color"]') !== null) {
      document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
    }
  }
  window.activateLightMode = function () {
    document.documentElement.setAttribute('data-theme', 'light')
   if (document.querySelector('meta[name="theme-color"]') !== null) {
      document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
    }
  }
  const autoChangeMode = '2'
  const t = saveToLocal.get('theme')
  if (autoChangeMode === '1') {
    const isDarkMode = window.matchMedia('(prefers-color-scheme: dark)').matches
    const isLightMode = window.matchMedia('(prefers-color-scheme: light)').matches
    const isNotSpecified = window.matchMedia('(prefers-color-scheme: no-preference)').matches
    const hasNoSupport = !isDarkMode && !isLightMode && !isNotSpecified
    if (t === undefined) {
      if (isLightMode) activateLightMode()
      else if (isDarkMode) activateDarkMode()
      else if (isNotSpecified || hasNoSupport) {
        const now = new Date()
        const hour = now.getHours()
        const isNight = hour <= 6 || hour >= 18
        isNight ? activateDarkMode() : activateLightMode()
      }
      window.matchMedia('(prefers-color-scheme: dark)').addListener(function (e) {
        if (saveToLocal.get('theme') === undefined) {
          e.matches ? activateDarkMode() : activateLightMode()
        }
      })
    } else if (t === 'light') activateLightMode()
    else activateDarkMode()
  } else if (autoChangeMode === '2') {
    const now = new Date()
    const hour = now.getHours()
    const isNight = hour <= 6 || hour >= 18
    if (t === undefined) isNight ? activateDarkMode() : activateLightMode()
    else if (t === 'light') activateLightMode()
    else activateDarkMode()
  } else {
    if (t === 'dark') activateDarkMode()
    else if (t === 'light') activateLightMode()
  }const asideStatus = saveToLocal.get('aside-status')
if (asideStatus !== undefined) {
   if (asideStatus === 'hide') {
     document.documentElement.classList.add('hide-aside')
   } else {
     document.documentElement.classList.remove('hide-aside')
   }
}})()</script><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/HCLonely/images@master/others/heartbeat.min.css"><link rel="stylesheet" href="/css/shuo.css"/><link rel="stylesheet" href="/magnetcss/magnet.css"/><link rel="stylesheet" href="/css/calendar.css"/><link rel="stylesheet" href="//at.alicdn.com/t/font_2060620_l0j6b710zk.css"/><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/sviptzk/StaticFile_HEXO@latest/butterfly/css/font-awesome-animation.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/sviptzk/StaticFile_HEXO@latest/butterfly/css/macblack.css"><link rel="stylesheet" href="/css/change.css"><link rel="stylesheet" href="/news/css/news.css"/><style type="text/css">#toggle-sidebar {bottom: 80px}</style><link rel="stylesheet" href="/magnet/css/catalogMagnet.css"/><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Zfour/Butterfly-double-row-display@1.00/cardlistpost.css"/><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/sviptzk/StaticFile_HEXO@latest/butterfly/css/iconfont.min.css"><link rel="stykesheet" href="https://cdn.jsdelivr.net/gh/sviptzk/StaticFile_HEXO@latest/butterfly/css/pool.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/font-awesome/css/font-awesome.min.css"/><link rel="stylesheet" href="/zan/zan.css"><link rel="stylesheet" href="/swiper/swiper.min.css"><link rel="stylesheet" href="/swiper/swiperstyle.css"><style>#article-container.post-content h1:before, h2:before, h3:before, h4:before, h5:before, h6:before { -webkit-animation: avatar_turn_around 1s linear infinite; -moz-animation: avatar_turn_around 1s linear infinite; -o-animation: avatar_turn_around 1s linear infinite; -ms-animation: avatar_turn_around 1s linear infinite; animation: avatar_turn_around 1s linear infinite; }</style><meta name="generator" content="Hexo 5.0.0"></head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="wizard-scene"><div class="wizard-objects"><div class="wizard-square"></div><div class="wizard-circle"></div><div class="wizard-triangle"></div></div><div class="wizard"><div class="wizard-body"></div><div class="wizard-right-arm"><div class="wizard-right-hand"></div></div><div class="wizard-left-arm"><div class="wizard-left-hand"></div></div><div class="wizard-head"><div class="wizard-beard"></div><div class="wizard-face"><div class="wizard-adds"></div></div><div class="wizard-hat"><div class="wizard-hat-of-the-hat"></div><div class="wizard-four-point-star --first"></div><div class="wizard-four-point-star --second"></div><div class="wizard-four-point-star --third"></div></div></div></div></div></div><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="author-avatar"><img class="avatar-img" data-lazy-src="http://01.027cgb.com/632500/1576904533264.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/archives/"><div class="headline">文章</div><div class="length-num">138</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/tags/"><div class="headline">标签</div><div class="length-num">63</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/categories/"><div class="headline">分类</div><div class="length-num">8</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/stars/"><i class="fa-fw fas fa-star faa-shake animated-hover"></i><span> 收藏网址</span></a></div><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw iconfont icon-zhuye faa-shake animated-hover"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/blog.html"><i class="fa-fw iconfont icon-Artboard faa-shake animated-hover"></i><span> 博客</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw iconfont icon-guidang faa-shake animated-hover"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw iconfont icon-biaoqian faa-shake animated-hover"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw iconfont icon-yule1"></i><span> 娱乐</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page" href="/movies/"><i class="fa-fw iconfont icon-yingshi faa-shake animated-hover"></i><span> 影视</span></a></li><li><a class="site-page" href="/games/"><i class="fa-fw iconfont icon-youxi1 faa-shake animated-hover"></i><span> 游戏</span></a></li><li><a class="site-page" href="/galleryGroup"><i class="fa-fw iconfont icon-tuku faa-shake animated-hover"></i><span> 图库</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/messagepad/"><i class="fa-fw iconfont icon-ziyuan6 faa-shake animated-hover"></i><span> 留言板</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw iconfont icon-icon_xinyong_xianxing_jijin- faa-shake animated-hover"></i><span> 友情链接</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw iconfont icon-guanyuwomen faa-shake animated-hover"></i><span> 关于我</span></a></div></div></div></div><div id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url(https://gitee.com/he_chaoming/blog_tuchuang/raw/master/20201207/Fl4X3LG@lWEY.jpg)"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">小何blog (记录美好)</a></span><span id="menus"><div id="search_button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/stars/"><i class="fa-fw fas fa-star faa-shake animated-hover"></i><span> 收藏网址</span></a></div><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw iconfont icon-zhuye faa-shake animated-hover"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/blog.html"><i class="fa-fw iconfont icon-Artboard faa-shake animated-hover"></i><span> 博客</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw iconfont icon-guidang faa-shake animated-hover"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw iconfont icon-biaoqian faa-shake animated-hover"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw iconfont icon-yule1"></i><span> 娱乐</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page" href="/movies/"><i class="fa-fw iconfont icon-yingshi faa-shake animated-hover"></i><span> 影视</span></a></li><li><a class="site-page" href="/games/"><i class="fa-fw iconfont icon-youxi1 faa-shake animated-hover"></i><span> 游戏</span></a></li><li><a class="site-page" href="/galleryGroup"><i class="fa-fw iconfont icon-tuku faa-shake animated-hover"></i><span> 图库</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/messagepad/"><i class="fa-fw iconfont icon-ziyuan6 faa-shake animated-hover"></i><span> 留言板</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw iconfont icon-icon_xinyong_xianxing_jijin- faa-shake animated-hover"></i><span> 友情链接</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw iconfont icon-guanyuwomen faa-shake animated-hover"></i><span> 关于我</span></a></div></div><span class="close" id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></span></span></nav><div id="post-info"><h1 class="post-title">Python基础知识点(爬虫之数据解析)</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2020-12-06T16:13:18.000Z" title="发表于 2020-12-07 00:13:18">2020-12-07</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2021-01-11T03:27:51.725Z" title="更新于 2021-01-11 11:27:51">2021-01-11</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E5%90%8E%E7%AB%AF%E5%AD%A6%E4%B9%A0/">后端学习</a></span></div><div class="meta-secondline"> <span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">3.1k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>13分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="Python基础知识点-爬虫之数据解析-："><a href="#Python基础知识点-爬虫之数据解析-：" class="headerlink" title="Python基础知识点(爬虫之数据解析)："></a>Python基础知识点(爬虫之数据解析)：</h1><blockquote>
<p><a target="_blank" rel="noopener" href="https://book.apeland.cn/details/12/">路飞学城Pythone文档</a></p>
</blockquote>
<ul>
<li><h2 id="引入："><a href="#引入：" class="headerlink" title="引入："></a>引入：</h2><ul>
<li><p>回顾<code>requests</code>模块实现数据爬取的流程</p>
<ul>
<li>指定url</li>
<li>发起请求</li>
<li>获取响应数据</li>
<li>持久化存储</li>
</ul>
</li>
<li><p>其实，在上述流程中还需要较为重要的一步，就是在持久化存储之前需要进行指定数据解析。因为大多数情况下的需求，我们都会指定去使用聚焦爬虫，也就是爬取页面中指定部分的数据值，而不是整个页面的数据。因此，本次课程中会给大家详细介绍讲解三种聚焦爬虫中的数据解析方式。至此，我们的数据爬取的流程可以修改为：</p>
<ul>
<li>指定url</li>
<li>发起请求</li>
<li>获取响应数据</li>
<li><strong>数据解析</strong></li>
<li>持久化存储</li>
</ul>
<h2 id=""><a href="#" class="headerlink" title=""></a><img src= "/img/loading.gif" data-lazy-src="https://book.apeland.cn/media/images/2019/04/15/snip20190415_19.png" alt="img"></h2></li>
</ul>
<hr>
</li>
<li><h2 id="python如何实现数据解析"><a href="#python如何实现数据解析" class="headerlink" title="python如何实现数据解析"></a><strong>python如何实现数据解析</strong></h2><ul>
<li><strong>正则表达式</strong></li>
<li><strong>xpath解析</strong></li>
<li><strong>bs4解析</strong></li>
</ul>
</li>
</ul>
<hr>
<ul>
<li><h2 id="常用正则表达式回顾"><a href="#常用正则表达式回顾" class="headerlink" title="常用正则表达式回顾:"></a>常用正则表达式回顾:</h2><p><img src= "/img/loading.gif" data-lazy-src="https://book.apeland.cn/media/images/2019/04/15/snip20190415_21.png"></p>
<ul>
<li><h3 id="贪婪模式和非贪婪模式："><a href="#贪婪模式和非贪婪模式：" class="headerlink" title="贪婪模式和非贪婪模式："></a>贪婪模式和非贪婪模式：</h3><ul>
<li><p>所谓贪婪匹配就是<strong>匹配重复字符是尽可能多的匹配</strong>，比如：</p>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="string">&quot;aaaaa&quot;</span>.match(<span class="regexp">/a+/</span>);</span><br><span class="line"><span class="comment">//[&quot;aaaaa&quot;, index: 0, input: &quot;aaaaa&quot;]</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>非贪婪匹配就是<strong>尽可能少的匹配，用法就是在量词后面加上一个“？”</strong>，比如：</p>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="string">&quot;aaaaa&quot;</span>.match(<span class="regexp">/a+?/</span>);</span><br><span class="line"><span class="comment">//[&quot;a&quot;, index: 0, input: &quot;aaaaa&quot;]</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>但是非贪婪匹配有时候和我们期待的并不一样，比如：</p>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="string">&quot;aaab&quot;</span>.match(<span class="regexp">/a+b/</span>);</span><br><span class="line">[<span class="string">&quot;aaab&quot;</span>, <span class="attr">index</span>: <span class="number">0</span>, <span class="attr">input</span>: <span class="string">&quot;aaab&quot;</span>]</span><br><span class="line"><span class="string">&quot;aaab&quot;</span>.match(<span class="regexp">/a+?b/</span>);</span><br><span class="line">[<span class="string">&quot;aaab&quot;</span>, <span class="attr">index</span>: <span class="number">0</span>, <span class="attr">input</span>: <span class="string">&quot;aaab&quot;</span>]</span><br></pre></td></tr></table></figure>
</li>
<li><p>在我们期待的情况下非贪婪匹配应该是匹配”ab”才对但是结果却和贪婪匹配时一样的。</p>
</li>
<li><p>在《权威指南》中有这么一句话：<strong>正则表达式的模式匹配总是会寻找字符串中第一个可能匹配的位置。</strong></p>
</li>
<li><p>个人对这句话的理解是：<strong>上例中正则表达式会先找到第一个字符a，因为a后面连接的字符有可能形成匹配，这是正则表达式就认定这个位置的字符了，然后开始往后进行匹配，如果像第一个例子中那样非贪婪匹配，匹配到第一个a就结束了，但是第二个例子中还要匹配b所以不得不接着往下匹配直到匹配到b为止结束。</strong></p>
</li>
</ul>
</li>
<li><h3 id="正则练习"><a href="#正则练习" class="headerlink" title="正则练习:"></a>正则练习:</h3><figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="keyword">import</span> re</span><br><span class="line"><span class="comment">#提取出python</span></span><br><span class="line">key=<span class="string">&quot;javapythonc++php&quot;</span></span><br><span class="line">re.findall(<span class="string">&#x27;python&#x27;</span>,key)[<span class="number">0</span>]</span><br><span class="line"><span class="comment">#####################################################################</span></span><br><span class="line"><span class="comment">#提取出hello world</span></span><br><span class="line">key=<span class="string">&quot;</span></span><br><span class="line"><span class="string">hello world</span></span><br><span class="line"><span class="string">&quot;</span></span><br><span class="line">re.findall(<span class="string">&#x27;(.*)&#x27;</span>,key)[<span class="number">0</span>]</span><br><span class="line"><span class="comment">#####################################################################</span></span><br><span class="line"><span class="comment">#提取170</span></span><br><span class="line">string = <span class="string">&#x27;我喜欢身高为170的女孩&#x27;</span></span><br><span class="line">re.findall(<span class="string">&#x27;\d+&#x27;</span>,string)</span><br><span class="line"><span class="comment">#####################################################################</span></span><br><span class="line"><span class="comment">#提取出http://和https://</span></span><br><span class="line">key=<span class="string">&#x27;http://www.baidu.com and https://boob.com&#x27;</span></span><br><span class="line">re.findall(<span class="string">&#x27;https?://&#x27;</span>,key)</span><br><span class="line"><span class="comment">#####################################################################</span></span><br><span class="line"><span class="comment">#提取出hello</span></span><br><span class="line">key=<span class="string">&#x27;lalalahellohahah&#x27;</span> <span class="comment">#输出hello</span></span><br><span class="line">re.findall(<span class="string">&#x27;&lt;[Hh][Tt][mM][lL]&gt;(.*)&#x27;</span>,key)</span><br><span class="line"><span class="comment">#####################################################################</span></span><br><span class="line"><span class="comment">#提取出hit. </span></span><br><span class="line">key=<span class="string">&#x27;bobo@hit.edu.com&#x27;</span><span class="comment">#想要匹配到hit.</span></span><br><span class="line">re.findall(<span class="string">&#x27;h.*?\.&#x27;</span>,key)</span><br><span class="line"><span class="comment">#####################################################################</span></span><br><span class="line"><span class="comment">#匹配sas和saas</span></span><br><span class="line">key=<span class="string">&#x27;saas and sas and saaas&#x27;</span></span><br><span class="line">re.findall(<span class="string">&#x27;sa&#123;1,2&#125;s&#x27;</span>,key)</span><br></pre></td></tr></table></figure>

<hr>
</li>
<li><h3 id="项目需求：爬取糗事百科指定页面的糗图，并将其保存到指定文件夹中"><a href="#项目需求：爬取糗事百科指定页面的糗图，并将其保存到指定文件夹中" class="headerlink" title="项目需求：爬取糗事百科指定页面的糗图，并将其保存到指定文件夹中:"></a>项目需求：爬取糗事百科指定页面的糗图，并将其保存到指定文件夹中:</h3><p><img src= "/img/loading.gif" data-lazy-src="https://book.apeland.cn/media/images/2019/04/15/snip20190415_24.png"></p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="comment">#-*- coding:utf-8 -*-</span></span><br><span class="line"><span class="comment">#@Time : 2020/12/6 13:48</span></span><br><span class="line"><span class="comment">#@Auther : 六根清静</span></span><br><span class="line"><span class="comment">#@File : __init__.py.py</span></span><br><span class="line"><span class="comment">#@Software: PyCharm</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> requests</span><br><span class="line"><span class="keyword">import</span> os</span><br><span class="line"><span class="keyword">import</span> re</span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&quot;__main__&quot;</span>:</span><br><span class="line">    headers = &#123;</span><br><span class="line">        <span class="string">&quot;User-Agent&quot;</span>: <span class="string">&quot;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.162 Safari/537.36&quot;</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">#指定起始也结束页码</span></span><br><span class="line">    page_start = int(input(<span class="string">&#x27;enter start page:&#x27;</span>))</span><br><span class="line">    page_end = int(input(<span class="string">&#x27;enter end page:&#x27;</span>))</span><br><span class="line">    <span class="comment">#创建文件夹</span></span><br><span class="line">    <span class="keyword">if</span> <span class="keyword">not</span> os.path.exists(<span class="string">&#x27;pic&#x27;</span>):</span><br><span class="line">        os.mkdir(<span class="string">&#x27;pic&#x27;</span>)</span><br><span class="line">    url = <span class="string">&quot;https://www.qiushibaike.com/imgrank/page/%d/&quot;</span></span><br><span class="line">    <span class="comment">#循环解析且下载指定页码中的图片数据</span></span><br><span class="line">    <span class="keyword">for</span> index <span class="keyword">in</span> range(page_start,page_end):</span><br><span class="line">        new_url = format(url%index)</span><br><span class="line">        page_text = requests.get(url=url,headers=headers).text</span><br><span class="line">        <span class="comment">#解析response中的图片链接</span></span><br><span class="line">        ex = <span class="string">&#x27;&lt;div class=&quot;thumb&quot;&gt;.*?&lt;img src=&quot;(.*?)&quot; alt.*?&lt;/div&gt;&#x27;</span></span><br><span class="line">        src_list = re.findall(ex,page_text,re.S)</span><br><span class="line">        <span class="comment">#循环下载该页码下所有的图片数据</span></span><br><span class="line">        <span class="keyword">for</span> src <span class="keyword">in</span> src_list:</span><br><span class="line">            src = <span class="string">&quot;https:&quot;</span> + src</span><br><span class="line">            img_data = requests.get(url=src,headers=headers).content</span><br><span class="line">            img_name = src.split(<span class="string">&#x27;/&#x27;</span>)[<span class="number">-1</span>]</span><br><span class="line">            img_path = <span class="string">&#x27;pic/&#x27;</span>+img_name</span><br><span class="line">            <span class="keyword">with</span> open(img_path,<span class="string">&#x27;wb&#x27;</span>) <span class="keyword">as</span> fp:</span><br><span class="line">                fp.write(img_data)</span><br><span class="line">                print(img_name + <span class="string">&#x27;下载成功!!!&#x27;</span>)</span><br></pre></td></tr></table></figure>

</li>
</ul>
<hr>
</li>
<li><h2 id="数据解析—bs4解析"><a href="#数据解析—bs4解析" class="headerlink" title="数据解析—bs4解析:"></a>数据解析—bs4解析:</h2><ul>
<li><h3 id="环境安装"><a href="#环境安装" class="headerlink" title="环境安装:"></a>环境安装:</h3><figure class="highlight python"><table><tr><td class="code"><pre><span class="line">- 需要将pip源设置为国内源，阿里源、豆瓣源、网易源等</span><br><span class="line">   - windows</span><br><span class="line">    （<span class="number">1</span>）打开文件资源管理器(文件夹地址栏中)</span><br><span class="line">    （<span class="number">2</span>）地址栏上面输入 %appdata%</span><br><span class="line">    （<span class="number">3</span>）在这里面新建一个文件夹  pip</span><br><span class="line">    （<span class="number">4</span>）在pip文件夹里面新建一个文件叫做  pip.ini ,内容写如下即可</span><br><span class="line">        [<span class="keyword">global</span>]</span><br><span class="line">        timeout = <span class="number">6000</span></span><br><span class="line">        index-url = https://mirrors.aliyun.com/pypi/simple/</span><br><span class="line">        trusted-host = mirrors.aliyun.com</span><br><span class="line">   - linux</span><br><span class="line">    （<span class="number">1</span>）cd ~</span><br><span class="line">    （<span class="number">2</span>）mkdir ~/.pip</span><br><span class="line">    （<span class="number">3</span>）vi ~/.pip/pip.conf</span><br><span class="line">    （<span class="number">4</span>）编辑内容，和windows一模一样</span><br><span class="line">- 需要安装：pip install bs4</span><br><span class="line">     bs4在使用时候需要一个第三方库，把这个库也安装一下</span><br><span class="line">     pip install lxml</span><br></pre></td></tr></table></figure>

<hr>
</li>
<li><h3 id="基础使用"><a href="#基础使用" class="headerlink" title="基础使用:"></a>基础使用:</h3><figure class="highlight python"><table><tr><td class="code"><pre><span class="line">使用流程：       </span><br><span class="line">    - 导包：<span class="keyword">from</span> bs4 <span class="keyword">import</span> BeautifulSoup</span><br><span class="line">    - 使用方式：可以将一个html文档，转化为BeautifulSoup对象，然后通过对象的方法或者属性去查找指定的节点内容</span><br><span class="line">        （<span class="number">1</span>）转化本地文件：</span><br><span class="line">             - soup = BeautifulSoup(open(<span class="string">&#x27;本地文件&#x27;</span>), <span class="string">&#x27;lxml&#x27;</span>)</span><br><span class="line">        （<span class="number">2</span>）转化网络文件：</span><br><span class="line">             - soup = BeautifulSoup(<span class="string">&#x27;字符串类型或者字节类型&#x27;</span>, <span class="string">&#x27;lxml&#x27;</span>)</span><br><span class="line">        （<span class="number">3</span>）打印soup对象显示内容为html文件中的内容</span><br><span class="line">基础巩固：</span><br><span class="line">    （<span class="number">1</span>）根据标签名查找</span><br><span class="line">        - soup.a   只能找到第一个符合要求的标签</span><br><span class="line">    （<span class="number">2</span>）获取属性</span><br><span class="line">        - soup.a.attrs  获取a所有的属性和属性值，返回一个字典</span><br><span class="line">        - soup.a.attrs[<span class="string">&#x27;href&#x27;</span>]   获取href属性</span><br><span class="line">        - soup.a[<span class="string">&#x27;href&#x27;</span>]   也可简写为这种形式</span><br><span class="line">    （<span class="number">3</span>）获取内容</span><br><span class="line">        - soup.a.string</span><br><span class="line">        - soup.a.text</span><br><span class="line">        - soup.a.get_text()</span><br><span class="line">       【注意】如果标签还有标签，那么string获取到的结果为<span class="literal">None</span>，而其它两个，可以获取文本内容</span><br><span class="line">    （<span class="number">4</span>）find：找到第一个符合要求的标签</span><br><span class="line">        - soup.find(<span class="string">&#x27;a&#x27;</span>)  找到第一个符合要求的</span><br><span class="line">        - soup.find(<span class="string">&#x27;a&#x27;</span>, title=<span class="string">&quot;xxx&quot;</span>)</span><br><span class="line">        - soup.find(<span class="string">&#x27;a&#x27;</span>, alt=<span class="string">&quot;xxx&quot;</span>)</span><br><span class="line">        - soup.find(<span class="string">&#x27;a&#x27;</span>, class_=<span class="string">&quot;xxx&quot;</span>)</span><br><span class="line">        - soup.find(<span class="string">&#x27;a&#x27;</span>, id=<span class="string">&quot;xxx&quot;</span>)</span><br><span class="line">    （<span class="number">5</span>）find_all：找到所有符合要求的标签</span><br><span class="line">        - soup.find_all(<span class="string">&#x27;a&#x27;</span>)</span><br><span class="line">        - soup.find_all([<span class="string">&#x27;a&#x27;</span>,<span class="string">&#x27;b&#x27;</span>]) 找到所有的a和b标签</span><br><span class="line">        - soup.find_all(<span class="string">&#x27;a&#x27;</span>, limit=<span class="number">2</span>)  限制前两个</span><br><span class="line">    （<span class="number">6</span>）根据选择器选择指定的内容</span><br><span class="line">               select:soup.select(<span class="string">&#x27;#feng&#x27;</span>)</span><br><span class="line">        - 常见的选择器：标签选择器(a)、类选择器(.)、id选择器(<span class="comment">#)、层级选择器</span></span><br><span class="line">            - 层级选择器：</span><br><span class="line">                div .dudu <span class="comment">#lala .meme .xixi  下面好多级</span></span><br><span class="line">                div &gt; p &gt; a &gt; .lala          只能是下面一级</span><br><span class="line">        【注意】select选择器返回永远是列表，需要通过下标提取指定的对象</span><br></pre></td></tr></table></figure>

<hr>
</li>
<li><h3 id="项目巩固"><a href="#项目巩固" class="headerlink" title="项目巩固:"></a>项目巩固:</h3><ul>
<li>需求：使用bs4实现将诗词名句网站中三国演义小说的每一章的内容爬去到本地磁盘进行存储<a target="_blank" rel="noopener" href="http://www.shicimingju.com/book/sanguoyanyi.html">http://www.shicimingju.com/book/sanguoyanyi.html</a></li>
</ul>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="comment">#-*- coding:utf-8 -*-</span></span><br><span class="line"><span class="comment">#@Time : 2020/12/6 19:10</span></span><br><span class="line"><span class="comment">#@Auther : 六根清静</span></span><br><span class="line"><span class="comment">#@File : __init__.py.py</span></span><br><span class="line"><span class="comment">#@Software: PyCharm</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> requests</span><br><span class="line"><span class="keyword">from</span> bs4 <span class="keyword">import</span> BeautifulSoup</span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&quot;__main__&quot;</span>:</span><br><span class="line">    url = <span class="string">&quot;https://www.shicimingju.com/book/sanguoyanyi.html&quot;</span></span><br><span class="line">    headers = &#123;</span><br><span class="line">        <span class="string">&quot;User-Agent&quot;</span>:<span class="string">&quot;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.162 Safari/537.36&quot;</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">#获取标题正文页数据</span></span><br><span class="line">    page_text = requests.get(url=url,headers=headers).text</span><br><span class="line">    <span class="comment">#创建soup对象</span></span><br><span class="line">    soup = BeautifulSoup(page_text,<span class="string">&#x27;lxml&#x27;</span>)</span><br><span class="line">    <span class="comment">#解析数据</span></span><br><span class="line">    li_list = soup.select(<span class="string">&#x27;.book-mulu &gt; ul &gt; li&#x27;</span>)</span><br><span class="line">    fp = open(<span class="string">&#x27;三国演义.txt&#x27;</span>,<span class="string">&#x27;w&#x27;</span>,encoding=<span class="string">&#x27;utf-8&#x27;</span>)</span><br><span class="line">    <span class="keyword">for</span> li <span class="keyword">in</span> li_list:</span><br><span class="line">        li_title = li.a.string</span><br><span class="line">        li_content_url = <span class="string">&#x27;https://www.shicimingju.com&#x27;</span> + li.a[<span class="string">&#x27;href&#x27;</span>]</span><br><span class="line">        li_detail = requests.get(url=li_content_url,headers=headers).text</span><br><span class="line">        detail_soup = BeautifulSoup(li_detail,<span class="string">&quot;lxml&quot;</span>)</span><br><span class="line">        <span class="comment">#解析获得标签中的数据值</span></span><br><span class="line">        detail_data = detail_soup.find(<span class="string">&#x27;div&#x27;</span>,class_ = <span class="string">&#x27;chapter_content&#x27;</span>).text</span><br><span class="line">        fp.write(li_title + <span class="string">&quot;:&quot;</span> + detail_data + <span class="string">&#x27;\n&#x27;</span>)</span><br><span class="line">        print(li_title + <span class="string">&#x27;爬取成功!&#x27;</span>)</span><br><span class="line">    fp.close()</span><br></pre></td></tr></table></figure>

</li>
</ul>
<hr>
</li>
<li><h2 id="数据解析—xpath解析"><a href="#数据解析—xpath解析" class="headerlink" title="数据解析—xpath解析:"></a>数据解析—xpath解析:</h2><ul>
<li><h3 id="引入"><a href="#引入" class="headerlink" title="引入:"></a>引入:</h3><ul>
<li><code>xpath</code>解析是我们在爬虫中<strong>最常用也是最通用的一种数据解析方式</strong>，由于其高效且简介的解析方式受到了广大程序员的喜爱。在后期学习<code>scrapy</code>框架期间，也会再次使用到<code>xpath</code>解析。</li>
</ul>
<hr>
</li>
<li><h3 id="环境安装-1"><a href="#环境安装-1" class="headerlink" title="环境安装:"></a>环境安装:</h3><ul>
<li><code>pip install lxml</code></li>
</ul>
<hr>
</li>
<li><h3 id="解析原理"><a href="#解析原理" class="headerlink" title="解析原理:"></a>解析原理:</h3><ul>
<li>使用通用爬虫爬取网页数据。</li>
<li>实例化<code>etree</code>对象，且将页面数据加载到该对象中。</li>
<li>使用<code>xpath</code>函数结合<code>xpath</code>表达式进行标签定位和指定数据提取。</li>
</ul>
<hr>
</li>
<li><h3 id="常用xpath表达式："><a href="#常用xpath表达式：" class="headerlink" title="常用xpath表达式："></a>常用xpath表达式：</h3><figure class="highlight"><table><tr><td class="code"><pre><span class="line">属性定位：</span><br><span class="line">    <span class="comment">#找到class属性值为song的div标签</span></span><br><span class="line">    //div[@class=&quot;song&quot;] </span><br><span class="line">层级&amp;索引定位：</span><br><span class="line">    <span class="comment">#找到class属性值为tang的div的直系子标签ul下的第二个子标签li下的直系子标签a</span></span><br><span class="line">    //div[@class=&quot;tang&quot;]/ul/li[2]/a</span><br><span class="line">逻辑运算：</span><br><span class="line">    <span class="comment">#找到href属性值为空且class属性值为du的a标签</span></span><br><span class="line">    //a[@href=&quot;&quot; and @class=&quot;du&quot;]</span><br><span class="line">模糊匹配：</span><br><span class="line">    //div[contains(@class, &quot;ng&quot;)]</span><br><span class="line">    //div[starts-with(@class, &quot;ta&quot;)]</span><br><span class="line">取文本：</span><br><span class="line">    <span class="comment"># /表示获取某个标签下的文本内容</span></span><br><span class="line">    <span class="comment"># //表示获取某个标签下的文本内容和所有子标签下的文本内容</span></span><br><span class="line">    //div[@class=&quot;song&quot;]/p[1]/text()</span><br><span class="line">    //div[@class=&quot;tang&quot;]//text()</span><br><span class="line">取属性：</span><br><span class="line">    //div[@class=&quot;tang&quot;]//li[2]/a/@href</span><br></pre></td></tr></table></figure>

<hr>
</li>
<li><h3 id="etree对象实例化"><a href="#etree对象实例化" class="headerlink" title="etree对象实例化:"></a><code>etree</code>对象实例化:</h3><figure class="highlight python"><table><tr><td class="code"><pre><span class="line">- 本地文件：tree = etree.parse(文件名)</span><br><span class="line">                tree.xpath(<span class="string">&quot;xpath表达式&quot;</span>)</span><br><span class="line">- 网络数据：tree = etree.HTML(网页内容字符串)</span><br><span class="line">                tree.xpath(<span class="string">&quot;xpath表达式&quot;</span>)</span><br></pre></td></tr></table></figure>

<hr>
</li>
<li><h3 id="实战案例"><a href="#实战案例" class="headerlink" title="实战案例:"></a>实战案例:</h3><ul>
<li><p>- 项目需求：解析58二手房的相关数据</p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="comment">#解析出一级页面的标题和二级页面的价格和描述</span></span><br><span class="line"><span class="keyword">import</span> requests</span><br><span class="line"><span class="keyword">from</span> lxml <span class="keyword">import</span> html</span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&quot;__main__&quot;</span>:</span><br><span class="line">    etree = html.etree</span><br><span class="line">    url = <span class="string">&quot;https://bj.58.com/ershoufang/&quot;</span></span><br><span class="line">    headers=&#123;</span><br><span class="line">        <span class="string">&quot;User-Agent&quot;</span>:<span class="string">&quot;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.162 Safari/537.36&quot;</span></span><br><span class="line">    &#125;</span><br><span class="line">    page_text = requests.get(url=url,headers=headers).text</span><br><span class="line">    tree = etree.HTML(page_text)</span><br><span class="line">    li_list = tree.xpath(<span class="string">&#x27;//ul[@class=&quot;house-list-wrap&quot;]/li&#x27;</span>)</span><br><span class="line">    fp = open(<span class="string">&#x27;58北京二手房.txt&#x27;</span>,<span class="string">&#x27;w&#x27;</span>,encoding=<span class="string">&#x27;utf-8&#x27;</span>)</span><br><span class="line">    <span class="keyword">for</span> li <span class="keyword">in</span> li_list:</span><br><span class="line">        <span class="comment">#解析标题</span></span><br><span class="line">        title = li.xpath(<span class="string">&#x27;./div[2]/h2/a/text()&#x27;</span>)[<span class="number">0</span>]</span><br><span class="line">        <span class="comment">#解析价格</span></span><br><span class="line">        price = li.xpath(<span class="string">&#x27;./div[3]/p[2]/text()&#x27;</span>)[<span class="number">0</span>]</span><br><span class="line">        print(price)</span><br><span class="line">        <span class="comment">#进行持久化存储</span></span><br><span class="line">        fp.write(title +<span class="string">&#x27;\t---------&gt;&#x27;</span>+price+<span class="string">&#x27;\n&#x27;</span>)</span><br><span class="line">    fp.close()</span><br><span class="line">    print(<span class="string">&quot;爬取成功!&quot;</span>)</span><br></pre></td></tr></table></figure>

<hr>
</li>
<li><p>- 项目需求：解析图片数据：<a target="_blank" rel="noopener" href="http://pic.netbian.com/4kmeinv/">http://pic.netbian.com/4kmeinv/</a></p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="keyword">import</span> requests</span><br><span class="line"><span class="keyword">from</span> lxml <span class="keyword">import</span> html</span><br><span class="line"><span class="keyword">import</span> os</span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&quot;__main__&quot;</span>:</span><br><span class="line">    <span class="comment">#判断是否存在指定文件夹</span></span><br><span class="line">    <span class="keyword">if</span> <span class="keyword">not</span> os.path.exists(<span class="string">&#x27;妹子图片&#x27;</span>):</span><br><span class="line">        <span class="comment">#创建指定文件夹</span></span><br><span class="line">        os.mkdir(<span class="string">&#x27;妹子图片&#x27;</span>)</span><br><span class="line">    etree = html.etree</span><br><span class="line">    url = <span class="string">&quot;http://pic.netbian.com/4kmeinv/&quot;</span></span><br><span class="line">    headers = &#123;</span><br><span class="line">        <span class="string">&quot;User-Agent&quot;</span>:<span class="string">&quot;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.162 Safari/537.36&quot;</span></span><br><span class="line">    &#125;</span><br><span class="line">    page_text = requests.get(url=url,headers=headers).text</span><br><span class="line">    tree = etree.HTML(page_text)</span><br><span class="line">    li_list = tree.xpath(<span class="string">&#x27;//div[@class=&quot;slist&quot;]/ul/li&#x27;</span>)</span><br><span class="line">    <span class="keyword">for</span> li <span class="keyword">in</span> li_list:</span><br><span class="line">        img_url = <span class="string">&#x27;http://pic.netbian.com&#x27;</span> + li.xpath(<span class="string">&#x27;./a/img/@src&#x27;</span>)[<span class="number">0</span>]</span><br><span class="line">        img_name = li.xpath(<span class="string">&#x27;./a/img/@alt&#x27;</span>)[<span class="number">0</span>] + <span class="string">&#x27;.jpg&#x27;</span></span><br><span class="line">        <span class="comment">#修改img_name的编码，gbk中文名称:汉字内码规范英文名称</span></span><br><span class="line">        img_name = img_name.encode(<span class="string">&#x27;iso-8859-1&#x27;</span>).decode(<span class="string">&#x27;gbk&#x27;</span>)</span><br><span class="line">        img_data = requests.get(url=img_url,headers=headers).content</span><br><span class="line">        file_name = <span class="string">&#x27;妹子图片/&#x27;</span> + img_name</span><br><span class="line">        <span class="keyword">with</span> open(file_name,<span class="string">&#x27;wb&#x27;</span>) <span class="keyword">as</span> fp:</span><br><span class="line">            fp.write(img_data)</span><br><span class="line">            print(img_name + <span class="string">&quot;爬取成功！&quot;</span>)</span><br></pre></td></tr></table></figure>
</li>
<li><p>- 项目需求：解析出所有城市名称<a target="_blank" rel="noopener" href="https://www.aqistudy.cn/historydata/">https://www.aqistudy.cn/historydata/</a></p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="keyword">import</span> requests</span><br><span class="line"><span class="keyword">from</span> lxml <span class="keyword">import</span> html</span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&quot;__main__&quot;</span>:</span><br><span class="line">    etree = html.etree</span><br><span class="line">    url = <span class="string">&quot;https://bj.58.com/ershoufang/&quot;</span></span><br><span class="line">    headers=&#123;</span><br><span class="line">        <span class="string">&quot;User-Agent&quot;</span>:<span class="string">&quot;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.162 Safari/537.36&quot;</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">#获取页面原始编码格式</span></span><br><span class="line">    page_text = requests.get(url=url,headers=headers).text</span><br><span class="line">    tree = etree.HTML(page_text)</span><br><span class="line">    li_list = tree.xpath(<span class="string">&#x27;//ul[@class=&quot;house-list-wrap&quot;]/li&#x27;</span>)</span><br><span class="line">    fp = open(<span class="string">&#x27;58北京二手房.txt&#x27;</span>,<span class="string">&#x27;w&#x27;</span>,encoding=<span class="string">&#x27;utf-8&#x27;</span>)</span><br><span class="line">    <span class="keyword">for</span> li <span class="keyword">in</span> li_list:</span><br><span class="line">        title = li.xpath(<span class="string">&#x27;./div[2]/h2/a/text()&#x27;</span>)[<span class="number">0</span>]</span><br><span class="line">        price = li.xpath(<span class="string">&#x27;./div[3]/p[2]/text()&#x27;</span>)[<span class="number">0</span>]</span><br><span class="line">        print(price)</span><br><span class="line">        fp.write(title +<span class="string">&#x27;\t---------&gt;&#x27;</span>+price+<span class="string">&#x27;\n&#x27;</span>)</span><br><span class="line">    fp.close()</span><br><span class="line">    print(<span class="string">&quot;爬取成功!&quot;</span>)</span><br></pre></td></tr></table></figure>
</li>
<li><p>- 项目需求：解析网页模板：<a target="_blank" rel="noopener" href="https://sc.chinaz.com/moban/">https://sc.chinaz.com/moban/</a></p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="keyword">import</span> requests</span><br><span class="line"><span class="keyword">from</span> lxml <span class="keyword">import</span> html</span><br><span class="line"><span class="keyword">import</span> os</span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&quot;__main__&quot;</span>:</span><br><span class="line">    <span class="keyword">if</span> <span class="keyword">not</span> os.path.exists(<span class="string">&#x27;网页模板&#x27;</span>):</span><br><span class="line">        os.mkdir(<span class="string">&#x27;网页模板&#x27;</span>)</span><br><span class="line">    etree = html.etree</span><br><span class="line">    url = <span class="string">&quot;https://sc.chinaz.com/moban/&quot;</span></span><br><span class="line">    headers = &#123;</span><br><span class="line">        <span class="string">&quot;User-Agent&quot;</span>:<span class="string">&quot;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.162 Safari/537.36&quot;</span></span><br><span class="line">    &#125;</span><br><span class="line">    response = requests.get(url=url,headers=headers)</span><br><span class="line">    <span class="comment">#修改页面编码</span></span><br><span class="line">    response.encoding = <span class="string">&quot;utf-8&quot;</span></span><br><span class="line">    page_text = response.text</span><br><span class="line">    tree = etree.HTML(page_text)</span><br><span class="line">    div_list = tree.xpath(<span class="string">&#x27;//div[@class=&quot;text_left&quot;]/div[@id=&quot;container&quot;]/div&#x27;</span>)</span><br><span class="line">    <span class="keyword">for</span> div <span class="keyword">in</span> div_list:</span><br><span class="line">        a_src = <span class="string">&quot;https:&quot;</span> + div.xpath(<span class="string">&#x27;./div/a/@href&#x27;</span>)[<span class="number">0</span>]</span><br><span class="line">        a_name = div.xpath(<span class="string">&#x27;./div/a/@alt&#x27;</span>)[<span class="number">0</span>] + <span class="string">&#x27;.rar&#x27;</span></span><br><span class="line">        response = requests.get(url=a_src,headers=headers)</span><br><span class="line">        response.encoding = <span class="string">&quot;utf-8&quot;</span></span><br><span class="line">        src_page_text = response.text</span><br><span class="line">        tree_src = etree.HTML(src_page_text)</span><br><span class="line">        down_url = tree_src.xpath(<span class="string">&#x27;//div[@class=&quot;downbody&quot;]/div[@class=&quot;dian&quot;][2]/a[1]/@href&#x27;</span>)[<span class="number">0</span>]</span><br><span class="line">        file_name = <span class="string">&#x27;网页模板/&#x27;</span>+ a_name</span><br><span class="line">        <span class="comment">#解析模板数据</span></span><br><span class="line">        template_data = requests.get(url=down_url,headers=headers).content</span><br><span class="line">        <span class="keyword">with</span> open(file_name,<span class="string">&#x27;wb&#x27;</span>) <span class="keyword">as</span> fp:</span><br><span class="line">            fp.write(template_data)</span><br><span class="line">            print(a_name + <span class="string">&#x27;爬取成功!&#x27;</span>)</span><br></pre></td></tr></table></figure>

</li>
</ul>
</li>
</ul>
<hr>
</li>
</ul>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">六根清静</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://liugenqingjing2.coding.me/2020/12/07/Python%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86%E7%82%B9-%E7%88%AC%E8%99%AB%E4%B9%8B%E6%95%B0%E6%8D%AE%E8%A7%A3%E6%9E%90/">https://liugenqingjing2.coding.me/2020/12/07/Python基础知识点-爬虫之数据解析/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://liugenqingjing2.coding.me" target="_blank">小何blog (记录美好)</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/Python/">Python</a><a class="post-meta__tags" href="/tags/%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86%E2%9C%8D%EF%B8%8F/">基础知识✍️</a></div><div class="post_share"><div class="social-share" data-image="https://gitee.com/he_chaoming/blog_tuchuang/raw/master/20201207/Fl4X3LG@lWEY.jpg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><div class="post-reward"><div class="reward-button"><i class="fas fa-qrcode"></i> 打赏<div class="reward-main"><ul class="reward-all"><li class="reward-item"><a href="http://qew58kkb2.hn-bkt.clouddn.com/wechat%20%20%281%29.png" target="_blank"><img class="post-qr-code-img" data-lazy-src="http://qew58kkb2.hn-bkt.clouddn.com/wechat%20%20%281%29.png" alt="微信"/></a><div class="post-qr-code-desc">微信</div></li></ul></div></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2020/12/13/Python%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86%E7%82%B9-%E7%88%AC%E8%99%AB%E4%B9%8B%E9%AA%8C%E8%AF%81%E7%A0%81%E8%AF%86%E5%88%AB/"><img class="prev-cover" data-lazy-src="http://ipo.027cgb.cn/632500/v2-9a7fe0facfe691120d3c30dfe5626889_720w.jpg" onerror="onerror=null;src='/img/404.jpg'"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">Python基础知识点(爬虫之验证码识别)</div></div></a></div><div class="next-post pull-right"><a href="/2020/12/05/Python%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86%E7%82%B9-%E7%88%AC%E8%99%AB%E4%B9%8Brequests%E6%A8%A1%E5%9D%97/"><img class="next-cover" data-lazy-src="https://gitee.com/he_chaoming/blog_tuchuang/raw/master/20201205/IGfP9VUx2RxJ.jpg" onerror="onerror=null;src='/img/404.jpg'"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">Python基础知识点(爬虫之requests模块)</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span> 相关推荐</span></div><div class="relatedPosts-list"><div><a href="/2020/11/22/Python基础知识点-一/" title="Python基础知识点(一)"><img class="cover" data-lazy-src="http://kp.027cgb.cn/632500/v2-20ea5bb113cc6d2e6f652c55d58d8873_r.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-11-22</div><div class="title">Python基础知识点(一)</div></div></a></div><div><a href="/2020/11/25/Python基础知识点-三/" title="Python基础知识点(三)"><img class="cover" data-lazy-src="https://gitee.com/he_chaoming/blog_tuchuang/raw/master/20201125/JDyfB^SVIBky.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-11-25</div><div class="title">Python基础知识点(三)</div></div></a></div><div><a href="/2020/11/30/Python基础知识点-七/" title="Python基础知识点(七.文件操作)"><img class="cover" data-lazy-src="https://gitee.com/he_chaoming/blog_tuchuang/raw/master/20201130/*pMaH7lvICSp.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-11-30</div><div class="title">Python基础知识点(七.文件操作)</div></div></a></div><div><a href="/2020/11/24/Python基础知识点-二/" title="Python基础知识点(二)"><img class="cover" data-lazy-src="http://jo.027cgb.cn/632500/v2-b340f24f677d70a4f10e52aee9c1a09c_r.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-11-24</div><div class="title">Python基础知识点(二)</div></div></a></div><div><a href="/2020/11/30/Python基础知识点-八-错误与异常/" title="Python基础知识点(八.错误与异常)"><img class="cover" data-lazy-src="https://gitee.com/he_chaoming/blog_tuchuang/raw/master/20201130/3*T4@zmMiVWJ.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-11-30</div><div class="title">Python基础知识点(八.错误与异常)</div></div></a></div><div><a href="/2020/11/27/Python基础知识点-五/" title="Python基础知识点(五)"><img class="cover" data-lazy-src="http://jo.027cgb.cn/632500/v2-8ec31945a8573cd2da251264e88f3843_r.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-11-27</div><div class="title">Python基础知识点(五)</div></div></a></div></div></div><hr/><div id="post-comment"><div class="comment-head"><div class="comment-headline"><i class="fas fa-comments fa-fw"></i><span> 评论</span></div></div><div class="comment-wrap"><div><div class="vcomment" id="vcomment"></div></div></div></div></div><div class="aside_content" id="aside_content"><div class="card-widget card-info"><div class="card-content"><div class="card-info-avatar is-center"><img class="avatar-img" data-lazy-src="http://01.027cgb.com/632500/1576904533264.jpg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/><div class="author-info__name">六根清静</div><div class="author-info__description">记录美好，记录生活，记录学习</div></div><div class="card-info-data"><div class="card-info-data-item is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">138</div></a></div><div class="card-info-data-item is-center"><a href="/tags/"><div class="headline">标签</div><div class="length-num">63</div></a></div><div class="card-info-data-item is-center"><a href="/categories/"><div class="headline">分类</div><div class="length-num">8</div></a></div></div><a class="button--animated" id="card-info-btn" target="_blank" rel="noopener" href="https://gitee.com/he_chaoming/dashboard/projects?scope=public&amp;&amp;sort="><i class="fab fa-gofore"></i><span>My Gitee</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://github.com/he1213114428" target="_blank" title="Github"><i class="fab fa-github"></i></a><a class="social-icon" href="mailto:484695754@qq.com" target="_blank" title="Email"><i class="fas fa-envelope"></i></a><a class="social-icon" href="tencent://message/?uin=484695754" target="_blank" title="QQ"><i class="fab fa-qq"></i></a></div></div></div><div class="card-widget card-recent-post"><div id="gamenews"><div class="card-content"><div class="item-headline"><i class="fas fa-cube"></i><span>ROLL资讯</span><i class="fas fa-retweet" @click="changetype(current)" style="float:right;margin-top:8px"></i></div><div class="newsbar"><span v-for="(type, index) in listtype" v-on:click="addClass(index)" v-bind:class="{ listtypeon:index==current}">{{type}}</span></div><div class="aside-list"><div class="aside-list-item" v-for="(news,index) in newsvue"><a class="thumbnail" href="javascript:void(0)" @click="getnewsdata(index)" :title="news.title"><img class="loaded" onerror="this.onerror=null;this.src='/img/404.jpg'" data-ll-status="loaded" :src="news.imgList[0]"/></a><div class="content"><a class="title" href="javascript:void(0)" @click="getnewsdata(index)" :title="news.title">{{news.title}}</a><time>{{news.postTime}}</time></div></div></div></div><div id="newsmodal"><button class="modalclose" @click="hidemodle()">关闭</button><h2>{{newpostvue.title}} </h2><h4>{{newpostvue.ptime}} &nbsp;&nbsp; {{newpostvue.source}}</h4><div v-html="newpostvue.content"></div></div></div></div><div class="card-widget card-shuo"><div class="card-content" style="height:auto;min-height:280px;"><div class="item-headline"><i class="fas fa-comments"></i><span>&#x8BF4;&#x8BF4</span><a id="shuovisualchange" style="cursor:pointer;float:right" onclick="shuovisualchange()">编辑</a></div><div id="artitalk_main" style="width:100%;height:100%;padding:10px"></div></div></div><div class="sticky_layout"></div><div class="card-widget card-history"><div class="card-content"><div class="item-headline"><i class="fas fa-paw fa-spin"></i><span>那年今日</span></div><div id="history-news" style="height: 100px;overflow: hidden;"><div id="history-card"></div></div></div></div><div class="card-widget card-announcement"><div class="card-content"><div class="item-headline"><i class="fas fa-bullhorn card-announcement-animation"></i><span>公告</span></div><div class="announcement_content">大学生一枚，软件工程，想做全栈攻城狮，哈哈，有兴趣可以加 QQ:484695754 (也可以点上面的logo)，一起交流学习</div></div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="card-content"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#Python%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86%E7%82%B9-%E7%88%AC%E8%99%AB%E4%B9%8B%E6%95%B0%E6%8D%AE%E8%A7%A3%E6%9E%90-%EF%BC%9A"><span class="toc-number">1.</span> <span class="toc-text">Python基础知识点(爬虫之数据解析)：</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%BC%95%E5%85%A5%EF%BC%9A"><span class="toc-number">1.1.</span> <span class="toc-text">引入：</span></a></li><li class="toc-item toc-level-2"><a class="toc-link"><span class="toc-number">1.2.</span> <span class="toc-text"></span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#python%E5%A6%82%E4%BD%95%E5%AE%9E%E7%8E%B0%E6%95%B0%E6%8D%AE%E8%A7%A3%E6%9E%90"><span class="toc-number">1.3.</span> <span class="toc-text">python如何实现数据解析</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%B8%B8%E7%94%A8%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F%E5%9B%9E%E9%A1%BE"><span class="toc-number">1.4.</span> <span class="toc-text">常用正则表达式回顾:</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%B4%AA%E5%A9%AA%E6%A8%A1%E5%BC%8F%E5%92%8C%E9%9D%9E%E8%B4%AA%E5%A9%AA%E6%A8%A1%E5%BC%8F%EF%BC%9A"><span class="toc-number">1.4.1.</span> <span class="toc-text">贪婪模式和非贪婪模式：</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%AD%A3%E5%88%99%E7%BB%83%E4%B9%A0"><span class="toc-number">1.4.2.</span> <span class="toc-text">正则练习:</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%A1%B9%E7%9B%AE%E9%9C%80%E6%B1%82%EF%BC%9A%E7%88%AC%E5%8F%96%E7%B3%97%E4%BA%8B%E7%99%BE%E7%A7%91%E6%8C%87%E5%AE%9A%E9%A1%B5%E9%9D%A2%E7%9A%84%E7%B3%97%E5%9B%BE%EF%BC%8C%E5%B9%B6%E5%B0%86%E5%85%B6%E4%BF%9D%E5%AD%98%E5%88%B0%E6%8C%87%E5%AE%9A%E6%96%87%E4%BB%B6%E5%A4%B9%E4%B8%AD"><span class="toc-number">1.4.3.</span> <span class="toc-text">项目需求：爬取糗事百科指定页面的糗图，并将其保存到指定文件夹中:</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%95%B0%E6%8D%AE%E8%A7%A3%E6%9E%90%E2%80%94bs4%E8%A7%A3%E6%9E%90"><span class="toc-number">1.5.</span> <span class="toc-text">数据解析—bs4解析:</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%8E%AF%E5%A2%83%E5%AE%89%E8%A3%85"><span class="toc-number">1.5.1.</span> <span class="toc-text">环境安装:</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%9F%BA%E7%A1%80%E4%BD%BF%E7%94%A8"><span class="toc-number">1.5.2.</span> <span class="toc-text">基础使用:</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%A1%B9%E7%9B%AE%E5%B7%A9%E5%9B%BA"><span class="toc-number">1.5.3.</span> <span class="toc-text">项目巩固:</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%95%B0%E6%8D%AE%E8%A7%A3%E6%9E%90%E2%80%94xpath%E8%A7%A3%E6%9E%90"><span class="toc-number">1.6.</span> <span class="toc-text">数据解析—xpath解析:</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%BC%95%E5%85%A5"><span class="toc-number">1.6.1.</span> <span class="toc-text">引入:</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%8E%AF%E5%A2%83%E5%AE%89%E8%A3%85-1"><span class="toc-number">1.6.2.</span> <span class="toc-text">环境安装:</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%A7%A3%E6%9E%90%E5%8E%9F%E7%90%86"><span class="toc-number">1.6.3.</span> <span class="toc-text">解析原理:</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%B8%B8%E7%94%A8xpath%E8%A1%A8%E8%BE%BE%E5%BC%8F%EF%BC%9A"><span class="toc-number">1.6.4.</span> <span class="toc-text">常用xpath表达式：</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#etree%E5%AF%B9%E8%B1%A1%E5%AE%9E%E4%BE%8B%E5%8C%96"><span class="toc-number">1.6.5.</span> <span class="toc-text">etree对象实例化:</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%9E%E6%88%98%E6%A1%88%E4%BE%8B"><span class="toc-number">1.6.6.</span> <span class="toc-text">实战案例:</span></a></li></ol></li></ol></li></ol></div></div></div><div class="card-widget card-recent-post"><div class="card-content"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/2021/03/21/Node-js%E5%AD%A6%E4%B9%A0-%E4%BA%8C%E5%8D%81%E4%B8%89-%E8%B7%A8%E5%9F%9F%E4%BB%8B%E7%BB%8D%E5%92%8CJSONP%E4%BB%8B%E7%BB%8D/" title="Node.js学习(二十三.跨域介绍和JSONP介绍)"><img data-lazy-src="https://gitee.com/he_chaoming/blog_tuchuang/raw/master/20210321/qWPEashJOuNK.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Node.js学习(二十三.跨域介绍和JSONP介绍)"/></a><div class="content"><a class="title" href="/2021/03/21/Node-js%E5%AD%A6%E4%B9%A0-%E4%BA%8C%E5%8D%81%E4%B8%89-%E8%B7%A8%E5%9F%9F%E4%BB%8B%E7%BB%8D%E5%92%8CJSONP%E4%BB%8B%E7%BB%8D/" title="Node.js学习(二十三.跨域介绍和JSONP介绍)">Node.js学习(二十三.跨域介绍和JSONP介绍)</a><time datetime="2021-03-21T13:40:58.000Z" title="发表于 2021-03-21 21:40:58">2021-03-21</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/03/20/Node-js%E5%AD%A6%E4%B9%A0-%E4%BA%8C%E5%8D%81%E4%BA%8C-Node-js%E9%A1%B9%E7%9B%AE%E6%B5%81%E7%A8%8B-%E4%B8%8B/" title="Node.js学习(二十二.Node.js项目流程(下))"><img data-lazy-src="https://gitee.com/he_chaoming/blog_tuchuang/raw/master/20210320/CvAoe6kC5*f2.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Node.js学习(二十二.Node.js项目流程(下))"/></a><div class="content"><a class="title" href="/2021/03/20/Node-js%E5%AD%A6%E4%B9%A0-%E4%BA%8C%E5%8D%81%E4%BA%8C-Node-js%E9%A1%B9%E7%9B%AE%E6%B5%81%E7%A8%8B-%E4%B8%8B/" title="Node.js学习(二十二.Node.js项目流程(下))">Node.js学习(二十二.Node.js项目流程(下))</a><time datetime="2021-03-20T14:33:29.000Z" title="发表于 2021-03-20 22:33:29">2021-03-20</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/03/15/Node-js%E5%AD%A6%E4%B9%A0-%E4%BA%8C%E5%8D%81%E4%B8%80-Node-js%E9%A1%B9%E7%9B%AE%E6%B5%81%E7%A8%8B-%E4%B8%AD/" title="Node.js学习(二十一.Node.js项目流程(中))"><img data-lazy-src="https://gitee.com/he_chaoming/blog_tuchuang/raw/master/20210320/gSrAfCznw@^F.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Node.js学习(二十一.Node.js项目流程(中))"/></a><div class="content"><a class="title" href="/2021/03/15/Node-js%E5%AD%A6%E4%B9%A0-%E4%BA%8C%E5%8D%81%E4%B8%80-Node-js%E9%A1%B9%E7%9B%AE%E6%B5%81%E7%A8%8B-%E4%B8%AD/" title="Node.js学习(二十一.Node.js项目流程(中))">Node.js学习(二十一.Node.js项目流程(中))</a><time datetime="2021-03-15T04:35:12.000Z" title="发表于 2021-03-15 12:35:12">2021-03-15</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/03/14/Node-js%E5%AD%A6%E4%B9%A0-%E4%BA%8C%E5%8D%81-Node-js%E9%A1%B9%E7%9B%AE%E6%B5%81%E7%A8%8B(%E4%B8%8A)/" title="Node.js学习(二十.Node.js项目流程(上))"><img data-lazy-src="https://gitee.com/he_chaoming/blog_tuchuang/raw/master/20210320/UMp@B^akHmf8.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Node.js学习(二十.Node.js项目流程(上))"/></a><div class="content"><a class="title" href="/2021/03/14/Node-js%E5%AD%A6%E4%B9%A0-%E4%BA%8C%E5%8D%81-Node-js%E9%A1%B9%E7%9B%AE%E6%B5%81%E7%A8%8B(%E4%B8%8A)/" title="Node.js学习(二十.Node.js项目流程(上))">Node.js学习(二十.Node.js项目流程(上))</a><time datetime="2021-03-14T14:36:58.000Z" title="发表于 2021-03-14 22:36:58">2021-03-14</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/02/27/Node-js%E5%AD%A6%E4%B9%A0-%E5%8D%81%E4%B9%9D-%E5%B0%81%E8%A3%85%E6%93%8D%E4%BD%9C%E6%95%B0%E6%8D%AE%E5%BA%93%E5%92%8CCSRF/" title="Node.js学习(十九.封装操作数据库和CSRF)"><img data-lazy-src="https://gitee.com/he_chaoming/blog_tuchuang/raw/master/20210227/EcgLTE4WPOp^.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Node.js学习(十九.封装操作数据库和CSRF)"/></a><div class="content"><a class="title" href="/2021/02/27/Node-js%E5%AD%A6%E4%B9%A0-%E5%8D%81%E4%B9%9D-%E5%B0%81%E8%A3%85%E6%93%8D%E4%BD%9C%E6%95%B0%E6%8D%AE%E5%BA%93%E5%92%8CCSRF/" title="Node.js学习(十九.封装操作数据库和CSRF)">Node.js学习(十九.封装操作数据库和CSRF)</a><time datetime="2021-02-27T13:20:18.000Z" title="发表于 2021-02-27 21:20:18">2021-02-27</time></div></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2021 By 六根清静</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div><div class="footer_custom_text">Hi,  welcome  to  my  <a  href="">blog</a>!</div><div class="icp"><a><img class="icp-icon" src="/img/icp.png" alt="ICP"/><span>豫ICP备2020025430号</span></a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="translateLink" type="button" title="简繁转换">简</button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="chat_btn" type="button" title="rightside.chat_btn"><i class="fas fa-sms"></i></button><a id="to_comment" href="#post-comment" title="直达评论"><i class="fas fa-comments"></i></a><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><div class="search-dialog__title" id="local-search-title">本地搜索</div><div id="local-input-panel"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div></div><hr/><div id="local-search-results"><div id="local-hits"></div><div id="local-stats"><div class="local-search-stats__hr" id="hr"><span>由</span> <a target="_blank" rel="noopener" href="https://github.com/wzpan/hexo-generator-search" style="color:#49B1F5;">hexo-generator-search</a>
 <span>提供支持</span></div></div></div><span class="search-close-button"><i class="fas fa-times"></i></span></div><div id="search-mask"></div></div><div><script src="https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js"></script><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="/js/tw_cn.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js"></script><script src="https://cdn.jsdelivr.net/npm/instant.page/instantpage.min.js" type="module" defer></script><script src="https://cdn.jsdelivr.net/npm/vanilla-lazyload/dist/lazyload.iife.min.js"></script><script src="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.js"></script><script src="/js/search/local-search.js"></script><script async="async">var preloader = {
  endLoading: () => {
    document.body.style.overflow = 'auto';
    document.getElementById('loading-box').classList.add("loaded")
  },
  initLoading: () => {
    document.body.style.overflow = '';
    document.getElementById('loading-box').classList.remove("loaded")

  }
}
window.addEventListener('load',()=> {preloader.endLoading()})
setTimeout(function(){preloader.endLoading();}, 50000);</script><div class="js-pjax"><script>function loadValine () {
  function initValine () {
    let initData = {
      el: '#vcomment',
      appId: 'kW2ft895T235y7DxmyJmXX5X-MdYXbMMI',
      appKey: 'v0zBPCAdwiJ0xg4s4eSfusLD',
      placeholder: '建议使用QQ邮箱，评论头像采用QQ头像',
      avatar: 'monsterid',
      meta: 'nick,mail,link'.split(','),
      pageSize: '10',
      lang: 'zh-CN',
      recordIP: false,
      serverURLs: 'https://???????.api.lncldglobal.com',
      emojiCDN: '',
      emojiMaps: "",
      enableQQ: true,
      path: window.location.pathname,
      master: '1f76478c4ec918ffc6d50de6b68db531'.split(','),
      friends: '5c?????bfe6rfc72a????e268ad3819c,7c?????bfe65fc02a????e2????3919c'.split(','),
      tagMeta: '博主,小伙伴,访客'.split(',')
    }

    if (true) { 
      initData.requiredFields= ('nick,mail'.split(','))
    }
    
    if (false) {
      const otherData = false
      initData = Object.assign({}, initData, otherData)
    }
    
    const valine = new Valine(initData)
  }

  if (typeof Valine === 'function') initValine() 
  else $.getScript('https://cdn.jsdelivr.net/gh/HCLonely/Valine@latest/dist/Valine.min.js', initValine)
}

if ('Valine' === 'Valine' || !true) {
  if (true) btf.loadComment(document.querySelector('#vcomment'),loadValine)
  else setTimeout(() => loadValine(), 0)
} else {
  function loadOtherComment () {
    loadValine()
  }
}</script><script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div><script src='https://cdn.jsdelivr.net/npm/artitalk'></script><script> $(".category-list-count").prepend("<i class=\"fas fa-book\"></i>");</script><script src="/js/list.js"></script><script src="/js/gitcalendar.js"></script><script src="/js/calendar.js"></script><script src="/js/xkTool.js"></script><script src="/js/pjax.js"></script><script src="/magnet/js/vue.min.js"></script><script src="/magnet/js/catalogMagnet.js"></script><script src="/js/airvisualapi.js"></script><script src="/js/history.js"></script><script src="/js/historyroll.js"></script><script src="/js/change.js"></script><script type="text/javascript" src="https://www.airvisual.com/scripts/widget_v2.0.js"></script><script> setTimeout(function(){$('.monoline').eq(0).html($(".monoline strong").eq(0).text().replace('°','℃'));},5000);</script><script src="https://cdn.jsdelivr.net/gh/sviptzk/HexoStaticFile@latest/Hexo/js/mouse_snow.min.js"></script><script src="/js/myBg.js"></script><script src="/js/shuo.js"></script><script src="/js/artitalkkey.js"></script><script src="/news/js/keys.js"></script><script src="/news/js/news.js"></script><script src="https://cdn.jsdelivr.net/gh/sviptzk/StaticFile_HEXO@latest/butterfly/js/pool.min.js"><script src="https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js"></script><script src="/stars/stars.js"></script><script src="https://cdn1.lncld.net/static/js/3.5.0/av-min.js"></script><script src="/zan/zan.js"></script><div class="aplayer no-destroy" data-id="2916766519" data-server="netease" data-type="playlist" data-fixed="true" data-mini="true" data-listFolded="false" data-order="random" data-preload="none" data-autoplay="false" muted></div><script src="/swiper/swiper.min.js"></script><script src="/swiper/swiperindex.js"></script><script async src="//at.alicdn.com/t/font_2264842_3izu8i5eoc2.js"></script><canvas class="fireworks" mobile="false"></canvas><script src="/js/third-party/fireworks.js"></script><script src="/js/third-party/activate-power-mode.js"></script><script>POWERMODE.colorful = true;
POWERMODE.shake = true;
POWERMODE.mobile = false;
document.body.addEventListener('input', POWERMODE);
</script><script>(function(i,s,o,g,r,a,m){i["DaoVoiceObject"]=r;i[r]=i[r]||function(){(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;a.charset="utf-8";m.parentNode.insertBefore(a,m)})(window,document,"script",('https:' == document.location.protocol ? 'https:' : 'http:') + "//widget.daovoice.io/widget/6fdec148.js","daovoice")
</script><script>var isChatBtn = true
daovoice('init', {
  app_id: '6fdec148',},{
  launcher: { 
     disableLauncherIcon: isChatBtn // 悬浮 ICON 是否显示
  },
});
daovoice('update');

if (isChatBtn) {
  var chatBtnFn = () => {
    var chatBtn = document.getElementById("chat_btn")
    chatBtn.addEventListener("click", function(){
      daovoice('show')
    });
  }
  chatBtnFn()
} else {
  if (true) {
    function chatBtnHide () {
      daovoice('update', {},{
        launcher: { 
        disableLauncherIcon: true // 悬浮 ICON 是否显示
        },
      });
    }
    function chatBtnShow () {
      daovoice('update', {},{
        launcher: { 
        disableLauncherIcon: false // 悬浮 ICON 是否显示
        },
      });
    }
  }
}</script><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.css"><script src="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.js"></script><script src="https://cdn.jsdelivr.net/gh/metowolf/MetingJS@1.2/dist/Meting.min.js"></script></div><script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"debug":false,"model":{"scale":1,"hHeadPos":0.5,"vHeadPos":0.618,"jsonPath":"/live2dw/assets/unitychan.model.json"},"display":{"superSample":2,"width":150,"height":300,"position":"right","hOffset":0,"vOffset":-20},"mobile":{"show":true,"scale":0.6},"react":{"opacityDefault":0.7,"opacityOnHover":0.8},"log":false});</script></body></html>